/*
@title: Chest Game
@description: Chest Game is a simple puzzle game where you push a box to reach a goal.
@author: Izaiaz2121
@tags: []
@addedOn: 2024-05-20
@img: ""
*/

const player = "p";
const box = "b";
const goal = "g";
const wall = "w";
const floor = "f";

const mytune = tune`
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + B5^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + A5^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G5^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + F5^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + E5^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + D5-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + C5-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + B4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + A4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G4/230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + F4/230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + E4/230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + F4^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G4^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + A4^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + B4^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + B4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + A4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + F4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + E4/230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + F4/230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G4/230.76923076923077,
230.76923076923077: C4~230.76923076923077 + D4~230.76923076923077 + A4^230.76923076923077,
230.76923076923077: C4~230.76923076923077 + D4~230.76923076923077 + B4^230.76923076923077,
230.76923076923077: C4~230.76923076923077 + D4~230.76923076923077 + C5^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + B4^230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + A4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + F4-230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + G4/230.76923076923077,
230.76923076923077: D4~230.76923076923077 + C4~230.76923076923077 + A4/230.76923076923077`;
const mysong = tune`
37.5: A5^37.5 + C4~37.5,
37.5: A5^37.5 + C4~37.5,
37.5: A5^37.5 + C4~37.5,
37.5: A5^37.5 + C4~37.5,
37.5: A5^37.5 + C4~37.5,
37.5: A5^37.5 + C4~37.5,
975`;

playTune(mytune);
const playback = playTune(mytune, 5);

setLegend(
  [ player, bitmap`
................
................
................
................
................
.....00000......
.....0C0C0......
......0C00......
......CCC.......
....LLLCLLL.....
....LLLLLLL.....
....L.LLL.L1....
....C.5551C1111.
......5.5..1....
......5.5.......
......0.0.......` ],
  [ box, bitmap`
6666666666666666
6CC66CC66CC66CC6
6CC66CC66CC66CC6
6CC66CC66CC66CC6
6CC66CC66CC66CC6
6CC66CC66CC66CC6
6CC66CC66CC66CC6
6666666666666666
6666666LL6666666
6CCCCCLCCLCCCCC6
6CCCCCLLLLCCCCC6
6CCCCCL11LCCCCC6
6CCCCCLLLLCCCCC6
6CCCCCCCCCCCCCC6
6CCCCCCCCCCCCCC6
6666666666666666` ],
  [ goal, bitmap`
................
................
................
...555....555...
...5L555555L5...
...55LLLLLL55...
....5L1111L5....
....5L1LL1L5....
....5L1LL1L5....
....5L1111L5....
...55LLLLLL55...
...5L555555L5...
...555....555...
................
................
................`],
  [ wall, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000` ],
  [ floor, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222` ]
);



let level = 0;
const levels = [
  map`
........
..ww....
p.w.b...
www.....
gwwwww..
........
........`,
  map`
pwg.....wwww........
.w..........b.......
.ww...w....w..wwww..
..wwwwwwww.ww......w
w.......w..wwww.....
www....bw.w...w...w.
...ww.........w..w..
....w..wwwww..w.....
ww..w..........ww.ww
.w..w..ww......ww...
.w.bw....www..ww....
....w......wwww.....
............w.w..www
..wwww..w...w.ww....
..w.....w..ww..w....
..w.....w.ww...wwww.
.ww....ww.w...ww....
ww....ww..wwww......
w..wwww.............
g..................g`,
  map`
......................
.b....................
w.wwwwwww.ww.w.w.wwww.
w.w...............w...
w.ww............w.....
w..w.w...........w....
w........w..w..w.w.ww.
.w.w...w.........w....
.........w..w...ww...w
......w........w....gw
.ww............w.w.ww.
.....ww..w...w.w.w....
www...w....w.....w....
....w.w...........ww..
.ww.w......w..........
...w.w...w......w.....
.w.w...w.w...w...w....
...w..w....w...w.....w
..ww...w.....w...w....
.ww..w..............w.
..wwwwwwwwwwwwwwwwwwww
....................p.`,
  map`
..........
.w..b..w..
..w.......
w....w..w.
...w......
..........
.wwww.....
...w...w..
....w...w.
..w...w...
..........
..p.g.....`,
  map`
..............
p.............
...w.w....w...
.wg..w.....w..
..ww.ww..w..ww
......w.......
.ww....w.ww.b.
..............`,
  map`
................
pb..............
wwwwwwwwwww...ww
.b..........w...
.......w....w...
.www..w.....ww.w
...w..ww.w......
.w.ww...w..w.ww.
....w..g.......g`,
  map`
......................w...
.b......w....w.....w...www
...w..w........w.w......w.
....w....w.....ww...www.gw
...w.......w.........w.w..
.....w.w..w....w.....w....
..w....w.....w.......w....
w.w..w.w.www.w...www....w.
.w.w..ww........w..w.....w
w........ww.w......w......
ww...w...ww.ww...w........
.ww...w.w.w......w....p...
ww.ww.......w..w.ww.......
.w.w.w..b..w..w..w.ww.....
...ww.....w.w......ww...ww
.......w.w..w..w..wwww....
..wwww..ww.....wwww.w....w
w.w.w..w......w..w........
..ww....w...ww.w..........
.w....w......w.w.....ww..w
...w....w.w.......w.......
gw......w...w.........w...
.....w..w.........w.w.w...
...w....w.w...w......w....`,
  map`
g.....
wwww..
.b...w
......
..p...
......`,
  map`
pw...w........
.....wg.......
..bw..........
..............
wwww.wwwwwww.w
g......w......
.....w....wb..
..w...w.......
........w.....`,
  map`
...........
.w..b..w...
.....w...w.
.w.w....w..
..w...w....
p.w..w.....
....w..w.w.
.....w...w.
w..w...g...`
  
];
const currentLevel = levels[level];
setMap(currentLevel);

setSolids([player,box,wall]);

setPushables({
  [ player ]: [box]
});

onInput("s", () => {
  getFirst(player).y += 1;
  playTune(mysong);
});

onInput("w", () => {
  getFirst(player).y -= 1;
  playTune(mysong);
});

onInput("d", () => {
  getFirst(player).x += 1;
  playTune(mysong);
});

onInput("a", () => {
  getFirst(player).x -= 1;
  playTune(mysong);
});

onInput("j", () => {
  const currentLevel = levels[level];

    if (currentLevel !== undefined) {
    clearText("");
    setMap(currentLevel);
    }
});
      
afterInput(() => {
  const targetNumber = tilesWith(goal).length;
  
  const numberCovered = tilesWith(goal, box).length;
  
  if (numberCovered === targetNumber) {

    level = level + 1;

    const currentLevel = levels[level];
     if (currentLevel !== undefined) {
      setMap(currentLevel);
    } else {
      addText("you win!", { y: 4, color: color`3` });
    }
  }

});
